#include "devinfo.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdbool.h>
#include <iostream>
#include <string>
#include <cstdio>
#include <memory>
#include <array>
#include "util.h"

// 获取当前音量百分比(0-100)
int DevInfo::get_volume() {
    std::string result = Util::exec_command("amixer get 'Power Amplifier' | grep 'Mono:' | awk '{print $3}' | tr -d '[]%'");
    int volume = Util::stringToIntStoi(result);
    return volume;
}
/**
 * 按照音量物理数值设置音量
 * @param volume
 * @return
 */
bool DevInfo::set_volume(int volume) {
    // 确保音量在合理范围内
    volume = std::max(0, std::min(63, volume));

    try {
        std::string cmd = "amixer set 'Power Amplifier' " + std::to_string(volume) ;
        Util::exec_command(cmd);
        std::cout << "Volume set to " << volume << "%" << std::endl;
    } catch (const std::exception& e) {
        std::cerr << "Failed to set volume: " << e.what() << std::endl;
    }
    return true;
}

/**
 * 按照百分比设置音量
 * @param volume
 * @return
 */
bool DevInfo::set_volume_percent(int volume) {
    // 确保音量在合理范围内
    volume = std::max(0, std::min(100, volume));

    try {
        std::string cmd = "amixer set 'Power Amplifier' " + std::to_string(volume) ;
        Util::exec_command(cmd);
        std::cout << "Volume set to " << volume << "%" << std::endl;
    } catch (const std::exception& e) {
        std::cerr << "Failed to set volume: " << e.what() << std::endl;
    }
    return true;
}

